package com.zhiyou100.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.entity.UserFollower;
import com.zhiyou100.entity.UserInfo;
import com.zhiyou100.service.inter.UserFollowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户模块 用户关注
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-06 18:42
 */
@RestController
public class UserFollowerController {
    @Autowired
    private UserFollowerService service;

    @GetMapping("/user/follower/select/list/{id}.action")
    public ResponseEntity<Object> selectListByUserId(@PathVariable String id) {
        if (ObjectUtil.isNull(id)) {
            // 为 null
            return new ResponseEntity<>(500, "id 为空", null);
        }
        // 根据 用户 id 查询
        final List<UserFollower> userFollowers = service.selectByMap(Dict.create().set("follower_user_id", id));
        if (ObjectUtil.isNull(userFollowers)) {
            //该用户没有任何关注 为 null
            return new ResponseEntity<>(500, "该用户没有任何关注", null);
        } else {
            return new ResponseEntity<>(200, "该用户的所有关注", userFollowers);
        }
    }

    @GetMapping("/user/follower/add.action")
    public ResponseEntity<Object> selectListByUserId(@RequestBody UserFollower userFollower, HttpServletRequest request) {
        if (ObjectUtil.isNull(userFollower)) {
            // 为空
            return new ResponseEntity<>(500, "userFollower 为空", null);
        }
        // 从域中获取 当前用户
        final UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        userFollower.setFollowerUserId(userInfo.getUserId());
        // 插入
        service.insert(userFollower);
        return new ResponseEntity<>(200, "添加关注成功！", userFollower);
    }

    @GetMapping("/user/follower/delete.action")
    public ResponseEntity<Object> delete(@RequestBody UserFollower userFollower) {
        if (ObjectUtil.isNull(userFollower)) {
            // 为空
            return new ResponseEntity<>(500, "userFollower 为空", null);
        }
        // 根据 实体类 删除
        service.delete(new UpdateWrapper<UserFollower>().setEntity(userFollower));
        return new ResponseEntity<>(200, "取消关注成功！", userFollower);
    }


}
